using System;
using System.Collections;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

using LocalBoard.Domain;

namespace LocalBoard.Persistence.SqlAccessor
{
	/// <summary>
	/// Summary description for SqlCommentAccessor.
	/// </summary>
	public class SqlCommentAccessor : SqlDataAccessBase, ICommentAccessor
	{
		public static readonly SqlCommentAccessor Instance = new SqlCommentAccessor();

		private SqlCommentAccessor()
		{
		}
		#region ICommentAccessor Members

		public System.Collections.ArrayList GetCommentListByMessageId(int messageId)
		{
			SqlDataReader dataReader = SqlHelper.ExecuteReader(ConnectionString, "GetCommentsByMessageId", messageId);
			SqlProfileAccessor userAccessor = SqlProfileAccessor.Instance;

			ArrayList commentList = new ArrayList();
			while(dataReader.Read())
			{
				commentList.Add(new Comment(DataConverter.GetInteger(dataReader["Id"]), DataConverter.GetString(dataReader["Body"]), DataConverter.GetString(dataReader["PostedByUserName"]), DataConverter.GetString(dataReader["PostedByUserId"]), DataConverter.GetDateTime(dataReader["Date"]), userAccessor.Get(DataConverter.GetInteger(dataReader["Author_Id"]))));
			}
			dataReader.Close();
			return commentList;
		}

		public int Insert(LocalBoard.Domain.Comment comment, int messageId)
		{
			return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, "InsertComment", comment.Body, comment.PostedByUserName, comment.PostedByUserId, comment.Author.Id, messageId));
		}

		public void Delete(int id)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, "DeleteComment", id);
		}

		#endregion
	}
}
